﻿using ConsoleApp.Util;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp.Infrastructure.Rmq
{
    public class RmqQueueManager
    {
        public static RmqOption Connect
        {
            get
            {
                return new RmqOption()
                {
                    Host = AppSettingsConstVars.Host,
                    Port = AppSettingsConstVars.Port,
                    VirtualHost = AppSettingsConstVars.VirtualHost,
                    Exchange = AppSettingsConstVars.Exchange,
                    UserName = AppSettingsConstVars.UserName,
                    Pwd = AppSettingsConstVars.Pwd
                };
            }
        }

        private static RmqHttpApiService _service = new RmqHttpApiService(Connect);

        public static void Enqueue<T>(T messageDTO, string queueName, int? expiration = null) where T : class
        {
            RmqMessageProducer.MessageSend(Connect, queueName, messageDTO, new RmqMessageProps()
            {
                DeliveryMode = 1,
                Expiration = expiration
            });
        }

        public static void Enqueue<T>(T messageDTO, string routeKey, RmqMessagePriorityLevel priorityLevel, int? expiration = null) where T : class
        {
            RmqMessageProducer.MessageSend(Connect, routeKey, messageDTO, new RmqMessageProps()
            {
                DeliveryMode = 1,
                Priority = priorityLevel,
                Expiration = expiration
            });
        }

        public static void EnqueueFromList<T>(List<T> messageDTOList, string queueName, int pageSize = 30, int? expiration = null) where T : class
        {
            if (messageDTOList.Count > 0)
            {
                var pageIndex = 1;
                while ((pageIndex - 1) * pageSize < messageDTOList.Count)
                {
                    try
                    {
                        var temp = messageDTOList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        RmqMessageProducer.MessageSend(Connect, queueName, temp, new RmqMessageProps()
                        {
                            DeliveryMode = 1,
                            Expiration = expiration
                        });

                        pageIndex++;
                    }
                    catch (Exception ex)
                    {
                        ex.ToString().WriteErrorLine();
                    }
                }
            }
        }

        /// <summary>
        /// 获取队列消息总数
        /// </summary>
        /// <param name="queueName"></param>
        /// <returns></returns>
        public static int GetQueueCount(string queueName)
        {
            return _service.GetMessageCount(queueName);
        }

        /// <summary>
        /// 获取节点信息
        /// </summary>
        /// <returns></returns>
        public static string GetNodesDetail()
        {
            return _service.GetNodesDetail();
        }
    }
}
